package ${ProxyPackage};

import com.askj.integration.infrastructure.converter.EntityConverter;
import com.askj.integration.infrastructure.proxy.AbstractMapperProxy;
import ${EntityPackage}.${EntityClassName};
import ${MapperPackage}.${entity}Mapper;
import ${POPackage}.${entity}PO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.mapstruct.Mapper;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.factory.Mappers;
import org.springframework.stereotype.Service;
import lombok.NonNull;
import java.util.Objects;
import java.util.Optional;

/**
 * @author ${author}
 */
@Slf4j
@Service
public class ${entity}Proxy extends AbstractMapperProxy<${EntityClassName}, ${entity}PO, ${entity}Mapper> {
    <#list table.fields as field><#if field.propertyName=='openId' && generate_query_by_openid>
    public Optional<${EntityClassName}> forOpenId(@NonNull String openId) {
        LambdaQueryWrapper<${entity}PO> wrapper = Wrappers.<${entity}PO>lambdaQuery()
                .eq(${entity}PO::getOpenId, openId);
        ${entity}PO po = mapper().selectOne(wrapper);
        if (Objects.nonNull(po)) {
            return Optional.of(converter().toEntity(po));
        }
        return Optional.empty();
    }
</#if></#list>
    @Override
    protected EntityConverter<${EntityClassName}, ${entity}PO> converter() {
        return Mappers.getMapper(${entity}Converter.class);
    }

    @Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
    interface ${entity}Converter extends EntityConverter<${EntityClassName}, ${entity}PO> {

    }
}